{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 424,
   "metadata": {},
   "outputs": [],
   "source": [
    "from utils import read_arff"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 数据处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 425,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>RI</th>\n",
       "      <th>Na</th>\n",
       "      <th>Mg</th>\n",
       "      <th>Al</th>\n",
       "      <th>Si</th>\n",
       "      <th>K</th>\n",
       "      <th>Ca</th>\n",
       "      <th>Ba</th>\n",
       "      <th>Fe</th>\n",
       "      <th>Type</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1.51793</td>\n",
       "      <td>12.79</td>\n",
       "      <td>3.50</td>\n",
       "      <td>1.12</td>\n",
       "      <td>73.03</td>\n",
       "      <td>0.64</td>\n",
       "      <td>8.77</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1.51643</td>\n",
       "      <td>12.16</td>\n",
       "      <td>3.52</td>\n",
       "      <td>1.35</td>\n",
       "      <td>72.89</td>\n",
       "      <td>0.57</td>\n",
       "      <td>8.53</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.00</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1.51793</td>\n",
       "      <td>13.21</td>\n",
       "      <td>3.48</td>\n",
       "      <td>1.41</td>\n",
       "      <td>72.64</td>\n",
       "      <td>0.59</td>\n",
       "      <td>8.43</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1.51299</td>\n",
       "      <td>14.40</td>\n",
       "      <td>1.74</td>\n",
       "      <td>1.54</td>\n",
       "      <td>74.55</td>\n",
       "      <td>0.00</td>\n",
       "      <td>7.59</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.00</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1.53393</td>\n",
       "      <td>12.30</td>\n",
       "      <td>0.00</td>\n",
       "      <td>1.00</td>\n",
       "      <td>70.16</td>\n",
       "      <td>0.12</td>\n",
       "      <td>16.19</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.24</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        RI     Na    Mg    Al     Si     K     Ca   Ba    Fe  Type\n",
       "0  1.51793  12.79  3.50  1.12  73.03  0.64   8.77  0.0  0.00     0\n",
       "1  1.51643  12.16  3.52  1.35  72.89  0.57   8.53  0.0  0.00     2\n",
       "2  1.51793  13.21  3.48  1.41  72.64  0.59   8.43  0.0  0.00     0\n",
       "3  1.51299  14.40  1.74  1.54  74.55  0.00   7.59  0.0  0.00     5\n",
       "4  1.53393  12.30  0.00  1.00  70.16  0.12  16.19  0.0  0.24     1"
      ]
     },
     "execution_count": 425,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = read_arff('./dataset/glass.arff')\n",
    "\n",
    "data.columns = [col_name[1:-1] for col_name in data.columns]\n",
    "data['Type'] = [type_name[1:-1] if ' ' in type_name else type_name for type_name in data['Type']]\n",
    "data['Type'] = data['Type'].replace({\n",
    "    \"build wind float\": 0,\n",
    "    \"build wind non-float\": 1,\n",
    "    \"vehic wind float\": 2,\n",
    "    \"vehic wind non-float\": 3,\n",
    "    \"containers\": 4,\n",
    "    \"tableware\": 5,\n",
    "    \"headlamps\": 6\n",
    "})\n",
    "data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 426,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['RI', 'Na', 'Mg', 'Al', 'Si', 'K', 'Ca', 'Ba', 'Fe', 'Type'], dtype='object')"
      ]
     },
     "execution_count": 426,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 427,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>RI</th>\n",
       "      <th>Na</th>\n",
       "      <th>Mg</th>\n",
       "      <th>Al</th>\n",
       "      <th>Si</th>\n",
       "      <th>K</th>\n",
       "      <th>Ca</th>\n",
       "      <th>Ba</th>\n",
       "      <th>Fe</th>\n",
       "      <th>Type</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "Empty DataFrame\n",
       "Columns: [RI, Na, Mg, Al, Si, K, Ca, Ba, Fe, Type]\n",
       "Index: []"
      ]
     },
     "execution_count": 427,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data[data['Type'] == 'vehic wind non-float']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 428,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>RI</th>\n",
       "      <th>Na</th>\n",
       "      <th>Mg</th>\n",
       "      <th>Al</th>\n",
       "      <th>Si</th>\n",
       "      <th>K</th>\n",
       "      <th>Ca</th>\n",
       "      <th>Ba</th>\n",
       "      <th>Fe</th>\n",
       "      <th>Type</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>214.000000</td>\n",
       "      <td>214.000000</td>\n",
       "      <td>214.000000</td>\n",
       "      <td>214.000000</td>\n",
       "      <td>214.000000</td>\n",
       "      <td>214.000000</td>\n",
       "      <td>214.000000</td>\n",
       "      <td>214.000000</td>\n",
       "      <td>214.000000</td>\n",
       "      <td>214.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>1.518365</td>\n",
       "      <td>13.407850</td>\n",
       "      <td>2.684533</td>\n",
       "      <td>1.444907</td>\n",
       "      <td>72.650935</td>\n",
       "      <td>0.497056</td>\n",
       "      <td>8.956963</td>\n",
       "      <td>0.175047</td>\n",
       "      <td>0.057009</td>\n",
       "      <td>1.780374</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>0.003037</td>\n",
       "      <td>0.816604</td>\n",
       "      <td>1.442408</td>\n",
       "      <td>0.499270</td>\n",
       "      <td>0.774546</td>\n",
       "      <td>0.652192</td>\n",
       "      <td>1.423153</td>\n",
       "      <td>0.497219</td>\n",
       "      <td>0.097439</td>\n",
       "      <td>2.103739</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>1.511150</td>\n",
       "      <td>10.730000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.290000</td>\n",
       "      <td>69.810000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>5.430000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>1.516522</td>\n",
       "      <td>12.907500</td>\n",
       "      <td>2.115000</td>\n",
       "      <td>1.190000</td>\n",
       "      <td>72.280000</td>\n",
       "      <td>0.122500</td>\n",
       "      <td>8.240000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>1.517680</td>\n",
       "      <td>13.300000</td>\n",
       "      <td>3.480000</td>\n",
       "      <td>1.360000</td>\n",
       "      <td>72.790000</td>\n",
       "      <td>0.555000</td>\n",
       "      <td>8.600000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>1.519157</td>\n",
       "      <td>13.825000</td>\n",
       "      <td>3.600000</td>\n",
       "      <td>1.630000</td>\n",
       "      <td>73.087500</td>\n",
       "      <td>0.610000</td>\n",
       "      <td>9.172500</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.100000</td>\n",
       "      <td>2.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>1.533930</td>\n",
       "      <td>17.380000</td>\n",
       "      <td>4.490000</td>\n",
       "      <td>3.500000</td>\n",
       "      <td>75.410000</td>\n",
       "      <td>6.210000</td>\n",
       "      <td>16.190000</td>\n",
       "      <td>3.150000</td>\n",
       "      <td>0.510000</td>\n",
       "      <td>6.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "               RI          Na          Mg          Al          Si           K  \\\n",
       "count  214.000000  214.000000  214.000000  214.000000  214.000000  214.000000   \n",
       "mean     1.518365   13.407850    2.684533    1.444907   72.650935    0.497056   \n",
       "std      0.003037    0.816604    1.442408    0.499270    0.774546    0.652192   \n",
       "min      1.511150   10.730000    0.000000    0.290000   69.810000    0.000000   \n",
       "25%      1.516522   12.907500    2.115000    1.190000   72.280000    0.122500   \n",
       "50%      1.517680   13.300000    3.480000    1.360000   72.790000    0.555000   \n",
       "75%      1.519157   13.825000    3.600000    1.630000   73.087500    0.610000   \n",
       "max      1.533930   17.380000    4.490000    3.500000   75.410000    6.210000   \n",
       "\n",
       "               Ca          Ba          Fe        Type  \n",
       "count  214.000000  214.000000  214.000000  214.000000  \n",
       "mean     8.956963    0.175047    0.057009    1.780374  \n",
       "std      1.423153    0.497219    0.097439    2.103739  \n",
       "min      5.430000    0.000000    0.000000    0.000000  \n",
       "25%      8.240000    0.000000    0.000000    0.000000  \n",
       "50%      8.600000    0.000000    0.000000    1.000000  \n",
       "75%      9.172500    0.000000    0.100000    2.000000  \n",
       "max     16.190000    3.150000    0.510000    6.000000  "
      ]
     },
     "execution_count": 428,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.describe()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 数据分析——KMeans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 429,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>RI</th>\n",
       "      <th>Na</th>\n",
       "      <th>Mg</th>\n",
       "      <th>Al</th>\n",
       "      <th>Si</th>\n",
       "      <th>K</th>\n",
       "      <th>Ca</th>\n",
       "      <th>Ba</th>\n",
       "      <th>Fe</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1.5214</td>\n",
       "      <td>13.8966</td>\n",
       "      <td>3.3431</td>\n",
       "      <td>1.0326</td>\n",
       "      <td>71.7914</td>\n",
       "      <td>0.1863</td>\n",
       "      <td>9.5509</td>\n",
       "      <td>0.0786</td>\n",
       "      <td>0.0480</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1.5163</td>\n",
       "      <td>14.6746</td>\n",
       "      <td>0.1654</td>\n",
       "      <td>2.1292</td>\n",
       "      <td>73.3138</td>\n",
       "      <td>0.0708</td>\n",
       "      <td>8.5804</td>\n",
       "      <td>0.9869</td>\n",
       "      <td>0.0150</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1.5173</td>\n",
       "      <td>13.1208</td>\n",
       "      <td>3.5024</td>\n",
       "      <td>1.3735</td>\n",
       "      <td>72.8181</td>\n",
       "      <td>0.5677</td>\n",
       "      <td>8.3963</td>\n",
       "      <td>0.0052</td>\n",
       "      <td>0.0655</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1.5283</td>\n",
       "      <td>11.8671</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>1.2186</td>\n",
       "      <td>71.6729</td>\n",
       "      <td>0.2514</td>\n",
       "      <td>14.3157</td>\n",
       "      <td>0.4500</td>\n",
       "      <td>0.1371</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1.5132</td>\n",
       "      <td>13.0100</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>3.0300</td>\n",
       "      <td>70.5900</td>\n",
       "      <td>6.2100</td>\n",
       "      <td>6.9450</td>\n",
       "      <td>0.0000</td>\n",
       "      <td>0.0000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>1.5201</td>\n",
       "      <td>13.1335</td>\n",
       "      <td>0.5729</td>\n",
       "      <td>1.4865</td>\n",
       "      <td>73.0682</td>\n",
       "      <td>0.5018</td>\n",
       "      <td>11.0053</td>\n",
       "      <td>0.0141</td>\n",
       "      <td>0.0618</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>1.5149</td>\n",
       "      <td>14.0067</td>\n",
       "      <td>3.0467</td>\n",
       "      <td>2.5100</td>\n",
       "      <td>71.3167</td>\n",
       "      <td>1.6333</td>\n",
       "      <td>5.6967</td>\n",
       "      <td>1.6733</td>\n",
       "      <td>0.0000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       RI       Na      Mg      Al       Si       K       Ca      Ba      Fe\n",
       "0  1.5214  13.8966  3.3431  1.0326  71.7914  0.1863   9.5509  0.0786  0.0480\n",
       "1  1.5163  14.6746  0.1654  2.1292  73.3138  0.0708   8.5804  0.9869  0.0150\n",
       "2  1.5173  13.1208  3.5024  1.3735  72.8181  0.5677   8.3963  0.0052  0.0655\n",
       "3  1.5283  11.8671  0.0000  1.2186  71.6729  0.2514  14.3157  0.4500  0.1371\n",
       "4  1.5132  13.0100  0.0000  3.0300  70.5900  6.2100   6.9450  0.0000  0.0000\n",
       "5  1.5201  13.1335  0.5729  1.4865  73.0682  0.5018  11.0053  0.0141  0.0618\n",
       "6  1.5149  14.0067  3.0467  2.5100  71.3167  1.6333   5.6967  1.6733  0.0000"
      ]
     },
     "execution_count": 429,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn.cluster import KMeans\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "kmeans = KMeans(n_clusters=7, n_init=12)\n",
    "kmeans_result = kmeans.fit_predict(data.iloc[:, :-1])\n",
    "kmeans.labels_\n",
    "pd.DataFrame(data=np.around(kmeans.cluster_centers_, decimals=4), columns=data.columns[:-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 430,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>RI</th>\n",
       "      <th>Na</th>\n",
       "      <th>Mg</th>\n",
       "      <th>Al</th>\n",
       "      <th>Si</th>\n",
       "      <th>K</th>\n",
       "      <th>Ca</th>\n",
       "      <th>Ba</th>\n",
       "      <th>Fe</th>\n",
       "      <th>Type</th>\n",
       "      <th>KMeans</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1.51793</td>\n",
       "      <td>12.79</td>\n",
       "      <td>3.50</td>\n",
       "      <td>1.12</td>\n",
       "      <td>73.03</td>\n",
       "      <td>0.64</td>\n",
       "      <td>8.77</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1.51643</td>\n",
       "      <td>12.16</td>\n",
       "      <td>3.52</td>\n",
       "      <td>1.35</td>\n",
       "      <td>72.89</td>\n",
       "      <td>0.57</td>\n",
       "      <td>8.53</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.00</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1.51793</td>\n",
       "      <td>13.21</td>\n",
       "      <td>3.48</td>\n",
       "      <td>1.41</td>\n",
       "      <td>72.64</td>\n",
       "      <td>0.59</td>\n",
       "      <td>8.43</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1.51299</td>\n",
       "      <td>14.40</td>\n",
       "      <td>1.74</td>\n",
       "      <td>1.54</td>\n",
       "      <td>74.55</td>\n",
       "      <td>0.00</td>\n",
       "      <td>7.59</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.00</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1.53393</td>\n",
       "      <td>12.30</td>\n",
       "      <td>0.00</td>\n",
       "      <td>1.00</td>\n",
       "      <td>70.16</td>\n",
       "      <td>0.12</td>\n",
       "      <td>16.19</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.24</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        RI     Na    Mg    Al     Si     K     Ca   Ba    Fe  Type  KMeans\n",
       "0  1.51793  12.79  3.50  1.12  73.03  0.64   8.77  0.0  0.00     0       2\n",
       "1  1.51643  12.16  3.52  1.35  72.89  0.57   8.53  0.0  0.00     2       2\n",
       "2  1.51793  13.21  3.48  1.41  72.64  0.59   8.43  0.0  0.00     0       2\n",
       "3  1.51299  14.40  1.74  1.54  74.55  0.00   7.59  0.0  0.00     5       1\n",
       "4  1.53393  12.30  0.00  1.00  70.16  0.12  16.19  0.0  0.24     1       3"
      ]
     },
     "execution_count": 430,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "kmeans_data = data.copy()\n",
    "kmeans_data['KMeans'] = kmeans.labels_\n",
    "kmeans_data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 431,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>RI</th>\n",
       "      <th>Na</th>\n",
       "      <th>Mg</th>\n",
       "      <th>Al</th>\n",
       "      <th>Si</th>\n",
       "      <th>K</th>\n",
       "      <th>Ca</th>\n",
       "      <th>Ba</th>\n",
       "      <th>Fe</th>\n",
       "      <th>Type</th>\n",
       "      <th>KMeans</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1.51793</td>\n",
       "      <td>12.79</td>\n",
       "      <td>3.50</td>\n",
       "      <td>1.12</td>\n",
       "      <td>73.03</td>\n",
       "      <td>0.64</td>\n",
       "      <td>8.77</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1.51643</td>\n",
       "      <td>12.16</td>\n",
       "      <td>3.52</td>\n",
       "      <td>1.35</td>\n",
       "      <td>72.89</td>\n",
       "      <td>0.57</td>\n",
       "      <td>8.53</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.00</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1.51793</td>\n",
       "      <td>13.21</td>\n",
       "      <td>3.48</td>\n",
       "      <td>1.41</td>\n",
       "      <td>72.64</td>\n",
       "      <td>0.59</td>\n",
       "      <td>8.43</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1.51299</td>\n",
       "      <td>14.40</td>\n",
       "      <td>1.74</td>\n",
       "      <td>1.54</td>\n",
       "      <td>74.55</td>\n",
       "      <td>0.00</td>\n",
       "      <td>7.59</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.00</td>\n",
       "      <td>5</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1.53393</td>\n",
       "      <td>12.30</td>\n",
       "      <td>0.00</td>\n",
       "      <td>1.00</td>\n",
       "      <td>70.16</td>\n",
       "      <td>0.12</td>\n",
       "      <td>16.19</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.24</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        RI     Na    Mg    Al     Si     K     Ca   Ba    Fe  Type  KMeans\n",
       "0  1.51793  12.79  3.50  1.12  73.03  0.64   8.77  0.0  0.00     0       1\n",
       "1  1.51643  12.16  3.52  1.35  72.89  0.57   8.53  0.0  0.00     2       1\n",
       "2  1.51793  13.21  3.48  1.41  72.64  0.59   8.43  0.0  0.00     0       1\n",
       "3  1.51299  14.40  1.74  1.54  74.55  0.00   7.59  0.0  0.00     5       6\n",
       "4  1.53393  12.30  0.00  1.00  70.16  0.12  16.19  0.0  0.24     1       1"
      ]
     },
     "execution_count": 431,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 大类匹配\n",
    "# 取预测类中真实类型的最大值为真实类\n",
    "mapped = {}\n",
    "for pre_num in range(7):\n",
    "    predict_true_nums = list(set(kmeans_data[kmeans_data['KMeans'] == pre_num]['Type']))\n",
    "    tomap = list(map(lambda x: list(kmeans_data[kmeans_data['KMeans'] == pre_num]['Type']).count(x), set(kmeans_data[kmeans_data['KMeans'] == pre_num]['Type'])))\n",
    "    mapped[pre_num] = predict_true_nums[tomap.index(max(tomap))]\n",
    "kmeans_data['KMeans'] = kmeans_data['KMeans'].replace(mapped)\n",
    "kmeans_data.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 数据分析——DBSCAN"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 432,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0,  0,  0, -1, -1,  0,  4,  0,  1,  0,  0, -1,  2,  2,  0,  0,  3,\n",
       "        0, -1,  0,  2, -1,  0,  0, -1,  0,  0,  3,  0,  0,  0,  0, -1,  0,\n",
       "        0,  0,  0, -1,  0, -1,  0,  0,  0,  0,  4,  0, -1,  0,  0,  0,  0,\n",
       "        0,  0,  0,  1,  0, -1,  2,  4,  0, -1,  4,  0,  2,  2, -1,  0,  2,\n",
       "       -1,  2,  6,  0, -1,  0,  2,  0,  0,  0,  0,  0,  0,  0,  2,  0, -1,\n",
       "       -1, -1,  0,  0, -1,  3,  0,  2, -1,  5,  2,  3, -1,  0,  0,  0,  0,\n",
       "       -1,  3,  3, -1,  0,  5,  0, -1, -1,  5, -1,  3,  0,  0, -1, -1, -1,\n",
       "       -1,  0,  0, -1, -1, -1,  0, -1, -1, -1,  0,  0,  0,  3,  0, -1,  0,\n",
       "        0,  0,  2,  0,  0,  0, -1, -1,  0,  0, -1, -1, -1,  0, -1,  2, -1,\n",
       "        0,  0,  0,  5, -1, -1,  0, -1,  2,  5, -1, -1,  2,  0,  0,  0,  5,\n",
       "        0,  0,  2, -1, -1, -1,  2,  0, -1, -1,  0,  0,  0, -1,  0,  3,  0,\n",
       "        0,  0, -1,  0,  0,  6,  1, -1,  0,  0,  0,  0,  0,  0,  0,  0,  0,\n",
       "        0,  0,  1,  0,  0,  0,  0,  0,  0,  6])"
      ]
     },
     "execution_count": 432,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn.cluster import DBSCAN\n",
    "\n",
    "dbscan = DBSCAN(eps=0.5, min_samples=3)\n",
    "dbscan_result = dbscan.fit_predict(data.iloc[:, :-1])\n",
    "dbscan.labels_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 433,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'clusters = 7, noise = 57'"
      ]
     },
     "execution_count": 433,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "clusters = len(set(dbscan.labels_)) - abs(dbscan.labels_.min())\n",
    "noise = list(dbscan.labels_).count(-1)\n",
    "f'clusters = {clusters}, noise = {noise}'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 434,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>RI</th>\n",
       "      <th>Na</th>\n",
       "      <th>Mg</th>\n",
       "      <th>Al</th>\n",
       "      <th>Si</th>\n",
       "      <th>K</th>\n",
       "      <th>Ca</th>\n",
       "      <th>Ba</th>\n",
       "      <th>Fe</th>\n",
       "      <th>Type</th>\n",
       "      <th>DBSCAN</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1.51793</td>\n",
       "      <td>12.79</td>\n",
       "      <td>3.50</td>\n",
       "      <td>1.12</td>\n",
       "      <td>73.03</td>\n",
       "      <td>0.64</td>\n",
       "      <td>8.77</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1.51643</td>\n",
       "      <td>12.16</td>\n",
       "      <td>3.52</td>\n",
       "      <td>1.35</td>\n",
       "      <td>72.89</td>\n",
       "      <td>0.57</td>\n",
       "      <td>8.53</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1.51793</td>\n",
       "      <td>13.21</td>\n",
       "      <td>3.48</td>\n",
       "      <td>1.41</td>\n",
       "      <td>72.64</td>\n",
       "      <td>0.59</td>\n",
       "      <td>8.43</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>1.51655</td>\n",
       "      <td>12.75</td>\n",
       "      <td>2.85</td>\n",
       "      <td>1.44</td>\n",
       "      <td>73.27</td>\n",
       "      <td>0.57</td>\n",
       "      <td>8.79</td>\n",
       "      <td>0.11</td>\n",
       "      <td>0.22</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>1.51779</td>\n",
       "      <td>13.64</td>\n",
       "      <td>3.65</td>\n",
       "      <td>0.65</td>\n",
       "      <td>73.00</td>\n",
       "      <td>0.06</td>\n",
       "      <td>8.93</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>2</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        RI     Na    Mg    Al     Si     K    Ca    Ba    Fe  Type  DBSCAN\n",
       "0  1.51793  12.79  3.50  1.12  73.03  0.64  8.77  0.00  0.00     0       0\n",
       "1  1.51643  12.16  3.52  1.35  72.89  0.57  8.53  0.00  0.00     2       0\n",
       "2  1.51793  13.21  3.48  1.41  72.64  0.59  8.43  0.00  0.00     0       0\n",
       "5  1.51655  12.75  2.85  1.44  73.27  0.57  8.79  0.11  0.22     1       0\n",
       "6  1.51779  13.64  3.65  0.65  73.00  0.06  8.93  0.00  0.00     2       4"
      ]
     },
     "execution_count": 434,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dbscan_data = data.copy()\n",
    "dbscan_data['DBSCAN'] = dbscan.labels_\n",
    "dbscan_data = dbscan_data[dbscan_data['DBSCAN'] >= 0]\n",
    "dbscan_data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 435,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>RI</th>\n",
       "      <th>Na</th>\n",
       "      <th>Mg</th>\n",
       "      <th>Al</th>\n",
       "      <th>Si</th>\n",
       "      <th>K</th>\n",
       "      <th>Ca</th>\n",
       "      <th>Ba</th>\n",
       "      <th>Fe</th>\n",
       "      <th>Type</th>\n",
       "      <th>DBSCAN</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1.51793</td>\n",
       "      <td>12.79</td>\n",
       "      <td>3.50</td>\n",
       "      <td>1.12</td>\n",
       "      <td>73.03</td>\n",
       "      <td>0.64</td>\n",
       "      <td>8.77</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1.51643</td>\n",
       "      <td>12.16</td>\n",
       "      <td>3.52</td>\n",
       "      <td>1.35</td>\n",
       "      <td>72.89</td>\n",
       "      <td>0.57</td>\n",
       "      <td>8.53</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1.51793</td>\n",
       "      <td>13.21</td>\n",
       "      <td>3.48</td>\n",
       "      <td>1.41</td>\n",
       "      <td>72.64</td>\n",
       "      <td>0.59</td>\n",
       "      <td>8.43</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>1.51655</td>\n",
       "      <td>12.75</td>\n",
       "      <td>2.85</td>\n",
       "      <td>1.44</td>\n",
       "      <td>73.27</td>\n",
       "      <td>0.57</td>\n",
       "      <td>8.79</td>\n",
       "      <td>0.11</td>\n",
       "      <td>0.22</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>1.51779</td>\n",
       "      <td>13.64</td>\n",
       "      <td>3.65</td>\n",
       "      <td>0.65</td>\n",
       "      <td>73.00</td>\n",
       "      <td>0.06</td>\n",
       "      <td>8.93</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        RI     Na    Mg    Al     Si     K    Ca    Ba    Fe  Type  DBSCAN\n",
       "0  1.51793  12.79  3.50  1.12  73.03  0.64  8.77  0.00  0.00     0       1\n",
       "1  1.51643  12.16  3.52  1.35  72.89  0.57  8.53  0.00  0.00     2       1\n",
       "2  1.51793  13.21  3.48  1.41  72.64  0.59  8.43  0.00  0.00     0       1\n",
       "5  1.51655  12.75  2.85  1.44  73.27  0.57  8.79  0.11  0.22     1       1\n",
       "6  1.51779  13.64  3.65  0.65  73.00  0.06  8.93  0.00  0.00     2       2"
      ]
     },
     "execution_count": 435,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 大类匹配\n",
    "# 取预测类中真实类型的最大值为真实类\n",
    "mapped = {}\n",
    "for pre_num in range(7):\n",
    "    predict_true_nums = list(set(dbscan_data[dbscan_data['DBSCAN'] == pre_num]['Type']))\n",
    "    tomap = list(map(lambda x: list(dbscan_data[dbscan_data['DBSCAN'] == pre_num]['Type']).count(x), set(dbscan_data[dbscan_data['DBSCAN'] == pre_num]['Type'])))\n",
    "    mapped[pre_num] = predict_true_nums[tomap.index(max(tomap))]\n",
    "dbscan_data['DBSCAN'] = dbscan_data['DBSCAN'].replace(mapped)\n",
    "dbscan_data.head()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 436,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.5934579439252337 0.5923566878980892\n",
      "0.44802023591378903 0.652896227095463\n"
     ]
    }
   ],
   "source": [
    "from sklearn.metrics import f1_score, accuracy_score\n",
    "\n",
    "print(accuracy_score(kmeans_data['Type'], kmeans_data['KMeans']), accuracy_score(dbscan_data['Type'], dbscan_data['DBSCAN']))\n",
    "print(f1_score(kmeans_data['Type'], kmeans_data['KMeans'], average='macro'), f1_score(dbscan_data['Type'], dbscan_data['DBSCAN'], average='macro'))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.10.6 ('env': venv)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.6"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "e6a44bc4ae6224bc4bc7b3a89841677b0124c79fe22f3d101cb1ffa8756f30f3"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
